//
//  BKUserModifyPwdView.m
//  bestkeep
//
//  Created by windCould on 16/12/13.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKUserModifyPwdView.h"
#import "BKMyCommon.h"

@interface BKUserModifyPwdView ()<UITextFieldDelegate>

@property (nonatomic,strong)UIImageView * leftImgV;
@property (nonatomic,strong)UIButton * rightBtn;

@end


@implementation BKUserModifyPwdView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setUpViews];
        self.backgroundColor = [UIColor whiteColor];
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textValueChanged) name:UITextFieldTextDidChangeNotification object:nil];
    }
    
    return  self;
}

-(void)setLeftImgUrlStr:(NSString *)leftImgUrlStr
{
    _leftImgV.image = [UIImage imageNamed:leftImgUrlStr];
}

-(void)setRightImgUrlStrs:(NSArray *)rightImgUrlStrs
{
    if (rightImgUrlStrs.count == 0 || rightImgUrlStrs == nil) {
        
        _rightBtn.hidden = YES;
    } else if (rightImgUrlStrs.count == 1){
    
        _rightBtn.hidden = NO;
        [_rightBtn setImage:[UIImage imageNamed:rightImgUrlStrs[0]] forState:UIControlStateNormal];
    } else {
    
        _rightBtn.hidden = NO;
        [_rightBtn setImage:[UIImage imageNamed:rightImgUrlStrs[0]] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:rightImgUrlStrs[1]] forState:UIControlStateSelected];
    }

}

#pragma mark =========================Function=============================

-(void)rightBtnClick:(UIButton *)btn
{
    btn.selected = !btn.isSelected;
    
    self.contentTexfield.secureTextEntry = !btn.isSelected;
}

-(void)textValueChanged
{
    NSInteger textLength = self.contentTexfield.text.length;
    if (textLength >18 ) {
        
        self.contentTexfield.text = [self.contentTexfield.text substringToIndex:18];
    }
}

#pragma mark ======================UITextFieldDelegate======================

-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    string = @"";
    if ([textField.text isEqualToString:@""] && range.length > 0) {
        //删除字符肯定是安全的
        return YES;
    }
    else {
        
        if (textField.text.length - range.length + string.length > 18) {
            
            return NO;
        }
        else {
            
            return YES;
        }
    }
    
}


#pragma mark =========================布局==================================
-(void)setUpViews{
    
    _leftImgV = [[UIImageView alloc]initWithFrame:CGRectZero];
    _leftImgV.contentMode = UIViewContentModeCenter;
    [self addSubview:_leftImgV];
    
    _contentTexfield = [[UITextField alloc]initWithFrame:CGRectZero];
    _contentTexfield = [[UITextField alloc]initWithFrame:CGRectZero];
    _contentTexfield.font = UIFontCustom(16);
    _contentTexfield.textColor = HexColor(0x5f646e);
    _contentTexfield.delegate = self;
    _contentTexfield.clearButtonMode = UITextFieldViewModeWhileEditing;
    _contentTexfield.returnKeyType = UIReturnKeyDefault;
    [self addSubview:_contentTexfield];
    
    _rightBtn = [UIButton buttonWithType: UIButtonTypeCustom];
    [_rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    _rightBtn.selected = NO;
    [self addSubview:_rightBtn];
    
    _bottomLineView= [[UIView alloc]init];
    _bottomLineView.backgroundColor = RGBCOLOR(221, 221, 221,1);
    [self addSubview:_bottomLineView];
    
    [self layoutViews];
}

-(void)layoutViews{
    
    
    [_leftImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(self.mas_left).offset(10);
        make.centerY.mas_equalTo(self.center.y).offset(0);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    [_contentTexfield mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(_leftImgV.mas_right).offset(10);
        make.centerY.mas_equalTo(_leftImgV.mas_centerY).offset(0);
        make.right.mas_equalTo(_rightBtn.mas_left).offset(-10);
        make.height.mas_equalTo(@(40));
    }];
    
    
    [_rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.centerY.mas_equalTo(_leftImgV.mas_centerY).offset(0);
        make.size.mas_equalTo(CGSizeMake(30, 20));
    }];
    
    
    [_bottomLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@(0.5));
        make.left.mas_equalTo(self.mas_left).offset(0);
        make.right.mas_equalTo(self.mas_right).offset(0);
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
    }];
    
    
}

-(void)dealloc
{
    [[NSNotificationCenter defaultCenter ]removeObserver:self name:UITextFieldTextDidChangeNotification object:nil];
}



@end
